/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.util;

import com.ejb.OperadorRemote;
import com.entidades.Aparelho;
import com.entidades.ParametrosSistema;
import java.util.List;
import java.util.Properties;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.rmi.PortableRemoteObject;

import javax.persistence.EntityManager;
import javax.persistence.Query;

/**
 *
 * @author Ricardo
 */
public class WorkerEJBUtil {

    public static OperadorRemote getOperadorBean(Aparelho ap, EntityManager em) throws NamingException {

        Properties props = new Properties();
        // props.setProperty("java.naming.factory.initial", " com.sun.enterprise.naming.SerialInitContextFactory");
        props.setProperty("java.naming.factory.url.pkgs", " com.sun.enterprise.naming");
        props.setProperty("java.naming.factory.state", " com.sun.corba.ee.impl.presentation.rmi.JNDIStateFactoryImpl");
        props.setProperty("org.omg.CORBA.ORBInitialHost", ap.getComputador().getIp());
        props.setProperty("org.omg.CORBA.ORBInitialPort", "3700");
        InitialContext ic = new InitialContext(props);
        //operador = (OperadorRemote) ic.lookup("java:comp/env/SMSChattyWorker/OperadorBean");
        Object obj = ic.lookup("OperadorBean");
        OperadorRemote operadorRemote = (OperadorRemote) PortableRemoteObject.narrow(obj, OperadorRemote.class);

        Query q = em.createQuery("select p from ParametrosSistema as p");


        List<ParametrosSistema> listaParametros = q.getResultList();

        Properties propsSistema = new Properties();
        for (ParametrosSistema parametro : listaParametros) {
            propsSistema.setProperty(parametro.getChave(), parametro.getValor());
        }
        operadorRemote.setParametrosSistema(propsSistema);

        return operadorRemote;

    }
}
